import pyodbc


def execute_sql_statement(sql_statement, connection_dict):
    """
    Executes an SQL statement and returns the results.

    :param sql_statement: The SQL statement to execute.
    :param connection_dict: A dictionary containing the connection details for the database.
    :return: A list of tuples containing the query results.
    """
    # Construct the connection string from the dictionary
    connection_string = (
        f"DRIVER={{MySQL ODBC 8.0 Unicode Driver}};"
        f"SERVER={{localhost}};"
        f"DATABASE={{world}};"
        f"UID={{root}};"
        f"PWD={{123456}};"
    )

    # Connect to the database using the connection string
    conn = pyodbc.connect(connection_string)

    # Create a cursor object to execute the SQL statement
    cursor = conn.cursor()

    # Execute the SQL statement
    cursor.execute(sql_statement)

    # Fetch the query results
    query_results = cursor.fetchall()

    # Close the cursor and connection
    cursor.close()
    conn.close()

    # Return the query results
    return query_results


# 设置数据库连接参数
conn_params = {
    'Driver': '{MySQL ODBC 8.0 Unicode Driver}',
    'Server': 'localhost',
    'Database': 'world',
    'UID': 'root',
    'PWD': '123456'
}


result_set = execute_sql_statement(
    "select code, name, continent from country", conn_params)

for r in result_set:
    print(r)
